package com.smart.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smart.common.PageInfo;
import com.smart.common.enums.OperateTypeEnum;
import com.smart.common.enums.SuperviseModuleEnum;
import com.smart.common.service.LogService;
import com.smart.common.utils.AssertUtil;
import com.smart.model.dtomapper.SuperviseUserDtoMapper;
import com.smart.model.domain.SuperviseUserDO;
import com.smart.mapper.SuperviseUserMapper;
import com.smart.model.request.SuperviseUserAddRequest;
import com.smart.model.request.SuperviseUserRequest;
import com.smart.model.request.SuperviseUserUpdateRequest;
import com.smart.service.SuperviseUserService;
import com.smart.model.convert.SuperviseUserConvert;
import com.smart.model.vo.SuperviseUserVO;
import com.smart.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class SuperviseUserServiceImpl extends ServiceImpl<SuperviseUserMapper, SuperviseUserDO> implements SuperviseUserService {
    @Autowired
    private SuperviseUserMapper superviseUserMapper;
    @Autowired
    private LogService logService;

    @Override
    public SuperviseUserVO getByKey(long id) {
        SuperviseUserDO superviseUserDO = getById(id);
        SuperviseUserVO superviseUserVO = SuperviseUserDtoMapper.INSTANCE.do2VO(superviseUserDO);
        return superviseUserVO;
    }

    @Override
    public PageInfo<SuperviseUserVO> pageQuery(SuperviseUserRequest request) {
        long count = superviseUserMapper.pageCount(request);
        List<SuperviseUserDO> list = superviseUserMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<SuperviseUserVO> superviseUserVOList = SuperviseUserConvert.buildSuperviseUserVoList(list);
        return new PageInfo(request.getPageNum(), request.getPageSize(), superviseUserVOList, count);
    }

    @Override
    public List<SuperviseUserVO> listQuery(SuperviseUserRequest request) {
        List<SuperviseUserDO> list = list();
        List<SuperviseUserVO> superviseUserVOS = SuperviseUserConvert.buildSuperviseUserVoList(list);
        return superviseUserVOS;
    }

    @Override
    public Boolean add(SuperviseUserAddRequest request) {
        List<String> allAccount = superviseUserMapper.selectAllAccount();
        AssertUtil.assertTrue(StringUtil.isValidAccount(request.getAccount()),"用户名为字母、数字或其组合");
        AssertUtil.assertTrue(!allAccount.contains(request.getAccount()),"用户名已存在,请更换一个");
        SuperviseUserDO superviseUserDO = SuperviseUserDtoMapper.INSTANCE.request2DO(request);
        save(superviseUserDO);

        //记录操作日志
        String afterContent = JSON.toJSONString(getById(superviseUserDO.getId()));
        logService.logSupervisePlatformAddScene(SuperviseModuleEnum.监管端管理人员, superviseUserDO.getId(), afterContent);
        return true;
    }

    @Override
    public Boolean updateByKey(SuperviseUserUpdateRequest request) {
        AssertUtil.assertNotNull(request.getId(),"id不能为空");
        AssertUtil.assertNotNull(request.getAccount(),"用户名不能为空");
        AssertUtil.assertNotNull(request.getPassword(),"密码不能为空");
        AssertUtil.assertNotNull(request.getPhone(),"手机号不能为空");
        SuperviseUserDO userDO = getById(request.getId());
        SuperviseUserDO superviseUserDO = SuperviseUserDtoMapper.INSTANCE.request2DO(request);
        superviseUserMapper.updateDO(superviseUserDO);
        //saveOrUpdate(superviseUserDO);

        //记录操作日志
        String beforeContent = JSON.toJSONString(userDO);
        String afterContent = JSON.toJSONString(getById(superviseUserDO.getId()));
        logService.logSupervisePlatformUpdateScene(SuperviseModuleEnum.监管端管理人员, superviseUserDO.getId(), beforeContent, afterContent);
        return true;
    }

    @Override
    public Boolean deleteByKey(long id) {
        SuperviseUserDO userDO = getById(id);
        superviseUserMapper.deleteDOById(id);
        //记录操作日志
        String beforeContent = JSON.toJSONString(userDO);
        logService.logSupervisePlatform(SuperviseModuleEnum.监管端管理人员, OperateTypeEnum.删除, id, beforeContent, null, "删除");
        return true;
    }
}
